//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Formatters;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace SoapTrace.Tests.HttpHeaderFormatting
{
	/// <summary>
	/// Formats a http header.
	/// </summary>
	[ TestFixture ]
	public class FieldFormatTestFixture
	{
		HttpHeaderFormatter Formatter;
		FormattedText FormattedText;
		
		string Text = "POST /webservices/latest.asmx HTTP/1.1\r\n" +
			"User-Agent: Mozilla/4.0\r\n" +
			"Content-Type: text/xml\r\n" +
			"\r\n";
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			Formatter = new HttpHeaderFormatter( );
			FormattedText = Formatter.Format( Text );
		}
		
		/// <summary>
		/// Checks the first field name.
		/// </summary>
		[ Test ]
		public void FirstFieldNameColour( )
		{
			Assert.AreEqual( Formatter.FieldNameColour, GetTextColour( "User-Agent", 40 ), 
			                "First field name text colour is wrong." );
		}	
		
		/// <summary>
		/// Checks the second field name.
		/// </summary>
		[ Test ]
		public void SecondFieldNameColour( )
		{
			Assert.AreEqual( Formatter.FieldNameColour, GetTextColour( "Content-Type", 65 ), 
			                "Second field name text colour is wrong." );
		}			
		
		/// <summary>
		/// Checks the first field's delimiter.
		/// </summary>
		[ Test ]
		public void FirstFieldDelimiterColour( )
		{
			Assert.AreEqual( Formatter.FieldDelimiterColour, GetTextColour( ":", 50 ), 
			                "First field delimiter text colour is wrong." );			
		}
		
		/// <summary>
		/// Checks the first field's value.
		/// </summary>
		[ Test ]
		public void FirstFieldValueColour( )
		{
			Assert.AreEqual( Formatter.FieldValueColour, GetTextColour( "Mozilla/4.0", 52 ), 
			                "First field value text colour is wrong." );			
		}	
		
		/// <summary>
		/// Gets the text colour at the specified index.
		/// </summary>
		/// <param name="text">The text that should be at the 
		/// specified index.</param>
		/// <param name="index">The character index.</param>
		private Color GetTextColour( string text, int index )
		{
			string MatchedText = FormattedText.Text.Substring( index, text.Length );
			
			Assert.AreEqual( text, MatchedText, "Unexpected text at index." );
			
			return FormattedText.GetColour( index, text.Length );
		}		
	}
}
